package com.jacobson.core.utils

import android.app.Activity
import android.app.Application
import android.content.ComponentCallbacks
import android.content.res.Configuration
import android.util.DisplayMetrics
import com.jacobson.core.widget.logger.LoggerFactory

/**
 * ================================================
 * 屏幕适配方案
 * @author Wucz on 2018/7/16 17:38
 * ================================================
 */
class Density {
    companion object {
        private const val HORIZONTAL = 0
        private const val VERTICAL = 1

        const val PORTRAIT = HORIZONTAL
        const val LANDSCAPE = VERTICAL

        private var appDensity: Float = 0.toFloat()
        private var appScaledDensity: Float = 0.toFloat()
        private var appDisplayMetrics: DisplayMetrics? = null

        fun setDensity(application: Application) {
            //获取application的DisplayMetrics
            appDisplayMetrics = application.resources.displayMetrics

            if (appDensity == 0f) {
                //初始化的时候赋值
                appDensity = appDisplayMetrics!!.density
                appScaledDensity = appDisplayMetrics!!.scaledDensity

                //添加字体变化的监听
                application.registerComponentCallbacks(object : ComponentCallbacks {
                    override fun onConfigurationChanged(newConfig: Configuration?) {
                        //字体改变后,将appScaledDensity重新赋值
                        if (newConfig != null && newConfig.fontScale > 0) {
                            appScaledDensity = application.resources.displayMetrics.scaledDensity
                        }
                    }

                    override fun onLowMemory() {}
                })
            }
        }

        //此方法在BaseActivity中做初始化(如果不封装BaseActivity的话,直接用下面那个方法就好了)
        fun setDefault(activity: Activity) {
            setOrientation(activity, HORIZONTAL)
        }

        //此方法用于在某一个Activity里面更改适配的方向
        fun setOrientation(activity: Activity, orientation: Int) {
            setAppOrientation(activity, orientation)
        }

        private fun setAppOrientation(activity: Activity, orientation: Int) {
            LoggerFactory.getLogger(Density::class.java).i(
                    "${activity.javaClass.simpleName} Set Density ${takeIf { orientation == VERTICAL }?.let { "VERTICAL" }
                            ?: "HORIZONTAL"}")
            val targetDensity: Float

            //获取状态栏高度
            val barHeight = AppUtils.getStatusBarHeight(activity)
            targetDensity = if (orientation == VERTICAL) {
                (appDisplayMetrics!!.heightPixels - barHeight) / 667f
            } else {
                appDisplayMetrics!!.widthPixels / 360f
            }

            val targetScaledDensity = targetDensity * (appScaledDensity / appDensity)
            val targetDensityDpi = (160 * targetDensity).toInt()

            /**
             *
             * 最后在这里将修改过后的值赋给系统参数
             *
             * 只修改Activity的density值
             */

            val activityDisplayMetrics = activity.resources.displayMetrics
            activityDisplayMetrics.density = targetDensity
            activityDisplayMetrics.scaledDensity = targetScaledDensity
            activityDisplayMetrics.densityDpi = targetDensityDpi
        }
    }

    annotation class DensityOrientation(
            val orientation: Int = PORTRAIT
    )

}